package com.panda.list.info;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.panda.list.utils.Utilities;

public class ApplicationInfo extends ItemInfo {

	public static final int TAG_NONE = 0x00;
	
	public static final int TAG_TV   = 0x01;
	public static final int TAG_EDU  = 0x02;
	public static final int TAG_VOD  = 0x04;

	public static final int TAG_APP = 0x05;


	public static final int PRELOAD = 0;
	public static final int THIRD_PARTY = 1;

	public int mTag;
	public Intent intent;
	public Bitmap bitmap;
	public Drawable draw;

	private long firstInstallTime;
	private boolean favorite;
	private long excuteCount;
	private CharSequence title;
	private int appType;
	private ComponentName componentName;

	public ApplicationInfo(Context context, PackageManager pm, ResolveInfo info) {

		final String packageName = info.activityInfo.applicationInfo.packageName;

		this.componentName = new ComponentName(packageName, info.activityInfo.name);
		this.setActivity(componentName,
				Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

		try {
			int appFlags = pm.getApplicationInfo(packageName, 0).flags;
			if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
				appType |= THIRD_PARTY;
			}

			//firstInstallTime = pm.getPackageInfo(packageName, 0).firstInstallTime;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		this.bitmap = Utilities.createIconBitmap(getFullResIcon(info, pm), context) ;
		this.title = info.activityInfo.loadLabel(pm).toString();
	}

	public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) {

		Drawable icon;
		Resources resources;
		try {
			resources = packageManager.getResourcesForApplication(info.activityInfo.applicationInfo);
			int iconId = info.activityInfo.getIconResource();
			icon = resources.getDrawable(iconId);
		} catch (Exception e) {
			icon = packageManager.getDefaultActivityIcon();
		}

		return icon;
	}

	final void setActivity(ComponentName className, int launchFlags) {
		intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setComponent(className);
		intent.setFlags(launchFlags);
	}

	public void setFirstInstallTime(long firstInstallTime) {
		this.firstInstallTime = firstInstallTime;
	}

	public long getFirstInstallTime() {
		return firstInstallTime;
	}

	public void setFavorite(boolean favorite) {
		this.favorite = favorite;
	}

	public boolean getFavorite() {
		return favorite;
	}

	public void setExcuteCount(long excuteCount) {
		this.excuteCount = excuteCount;
	}

	public long getExcuteCount() {
		return excuteCount;
	}

	public void setTitle(CharSequence title) {
		this.title = title;
	}

	public CharSequence getTitle() {
		return title;
	}

	public void setAppType(int appType) {
		this.appType = appType;
	}

	public int getAppType() {
		return appType;
	}

	public void setTag(int tag) {
		this.mTag = tag; 
	}

	public boolean isCurrentTag(int tag) {
		int sumTag = this.mTag - tag;

		if ( sumTag < 0 )
			return false;
		else if ( sumTag == 0 )
			return true;
		else {
			switch (tag) {
			case TAG_TV:
				if ( sumTag == TAG_EDU || sumTag == TAG_VOD || sumTag == (TAG_EDU + TAG_VOD)) return true; 
				break;
			case TAG_EDU:
				if ( sumTag == TAG_TV || sumTag == TAG_VOD || sumTag == (TAG_TV + TAG_VOD)) return true;
				break;
			case TAG_VOD:
				if ( sumTag == TAG_TV || sumTag == TAG_EDU || sumTag == (TAG_TV + TAG_EDU)) return true;
				break;
			}
		}
		return false;
	}

	@Override
	public boolean isTag() {
		return false;
	}
}
